const Setting = require('./src/setting.env')

// 拼接路径
const resolve = dir => require('path').join(__dirname, dir)
const {
  VUE_APP_DEV_HOST,
  VUE_APP_API_DOMAIN_PRODUCT,
  VUE_APP_API_DOMAIN_USERCENTER,
  VUE_APP_API_DOMAIN_SUPPLIER,
  VUE_APP_API_DOMAIN_SUPPLIER_EXPORT,
} = process.env

// 增加环境变量
process.env.VUE_APP_VERSION = require('./package.json').version
process.env.VUE_APP_BUILD_TIME = require('dayjs')().format('YYYY-M-D HH:mm:ss')

module.exports = {
  publicPath: Setting.publicPath,
  lintOnSave: Setting.lintOnSave,
  outputDir: Setting.outputDir,
  assetsDir: Setting.assetsDir,
  runtimeCompiler: true,
  // 如果您不需要生产时的源映射，那么将此设置为false可以加速生产构建
  productionSourceMap: false,
  devServer: {
    host: VUE_APP_DEV_HOST,
    disableHostCheck: true,
    publicPath: Setting.publicPath,
    proxy: {
      '/purchase/': {
        target: VUE_APP_API_DOMAIN_PRODUCT,
        secure: false,
        changeOrigin: true,
      },
      '/usercenter': {
        target: VUE_APP_API_DOMAIN_USERCENTER,
        changeOrigin: true,
      },
      '/sysback': {
        target: VUE_APP_API_DOMAIN_PRODUCT,
        ws: true, // 是否启用websockets
        secure: false, // 使用的是http协议则设置为false，https协议则设置为true
        // 开启代理：在本地会创建一个虚拟服务端，然后发送请求的数据，并同时接收请求的数据，这样客户端端和服务端进行数据的交互就不会有跨域问题
        changOrigin: true
      },
      '/dataBatch': {
        target: VUE_APP_API_DOMAIN_PRODUCT,
        ws: true, // 是否启用websockets
        secure: false, // 使用的是http协议则设置为false，https协议则设置为true
        changOrigin: true
      },
      '/srm-api': {
        target: VUE_APP_API_DOMAIN_PRODUCT,
        // target: 'http://192.168.5.117:9098/',
        changeOrigin: true,
      },
      '/fts': {
        target: VUE_APP_API_DOMAIN_PRODUCT,
        changeOrigin: true,
      },
      '/modular': {
        target: 'http://192.168.0.105:6108/',
        changeOrigin: true,
      },
      '/srm-admin': {
        target: VUE_APP_API_DOMAIN_SUPPLIER,
        changeOrigin: true,
      },
      '/srm-export': {
        target: VUE_APP_API_DOMAIN_SUPPLIER_EXPORT,
        changeOrigin: true,
      },
    }
  },
  css: {
    loaderOptions: {
      less: {}
    }
  },
  transpileDependencies: ['view-design'],
  // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-service/lib/config/base.js
  chainWebpack: config => {
    /**
     * 删除懒加载模块的 prefetch preload，降低带宽压力
     * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
     * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
     * 而且预渲染时生成的 prefetch 标签是 modern 版本的，低版本浏览器是不需要的
     */

    config.plugins.delete('prefetch').delete('preload')
    // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
    config.resolve.symlinks(true)
    config
      // 开发环境
      .when(
        process.env.NODE_ENV === 'development',
        // sourcemap不包含列信息
        config => config.devtool('source-map')
      )
      // 非开发环境
      .when(process.env.NODE_ENV !== 'development', config => {})
    // 不编译 iView Pro
    config.module
      .rule('js')
      .test(/\.jsx?$/)
      .exclude.add(resolve('src/libs/iview-pro'))
      .end()
    // 使用 iView Loader
    config.module
      .rule('vue')
      .test(/\.vue$/)
      .use('iview-loader')
      .loader('iview-loader')
      .tap(() => {
        return Setting.iviewLoaderOptions
      })
      .end()
    // markdown
    config.module
      .rule('md')
      .test(/\.md$/)
      .use('text-loader')
      .loader('text-loader')
      .end()
    // i18n
    config.module
      .rule('i18n')
      .resourceQuery(/blockType=i18n/)
      .use('i18n')
      .loader('@kazupon/vue-i18n-loader')
      .end()
    // image exclude
    const imagesRule = config.module.rule('images')
    imagesRule
      .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
      .exclude.add(resolve('src/assets/svg'))
      .end()
    // 重新设置 alias
    config.resolve.alias
      .set('@api', resolve('src/api'))
      .set('@sourcing', resolve('src/pages/sourcing'))
      .set('@supplier', resolve('src/pages/supplier'))
      .set('@purchase', resolve('src/pages/purchase'))
      .set('@stock', resolve('src/pages/stock'))
    // node
    config.node.set('__dirname', true).set('__filename', true)
    // 判断是否需要加入模拟数据
    const entry = config.entry('app')
    if (Setting.isMock) {
      entry.add('@/mock').end()
    }
  },
  configureWebpack: {
    watch: true,
    devtool: 'source-map',
    performance: {
      hints: false // 性能提示 false
    },
    resolve: {
      alias: {
        '@': resolve('src'),
      }
    }
  }
}
